import GlobalDialog from "@common/components/dialog/global";
import QrCode from "./QrCode";

const genButtons = options => {
    let buttons = [];
    buttons.push({
        text: '关 闭',
        click(dialog) {
            dialog.visible = false;
        },
    });
    if (options.showDownload) {
        buttons.push({
            type: 'primary',
            text: '下载',
            click(dialog, { component }) {
                component && typeof component.download === 'function' && component.download();
            },
        });
    }
    buttons.push({
        type: 'primary',
        text: '打印',
        click(dialog, { component }) {
            component && typeof component.doPrint === 'function' && component.doPrint();
        }
    });
    return buttons;
};
const QrCodeDialog = (store, router) => {
    let dialog = GlobalDialog(store, router);
    return options => {
        const dialogVm = dialog({
            title: '二维码',
            width: options.width || 'calc(210mm + 10px)',
            component: {
                value: QrCode,
                props: {
                    data: options.data,
                    pageSize: options.pageSize,
                    size: options.size,
                    pageStyle: options.pageStyle,
                },
                event(event) {
                    if (typeof options.event === 'function') {
                        options.event(event, dialogVm);
                    }
                },
            },
            buttons: genButtons(options),
        });
        return dialogVm;
    };
};
export default QrCodeDialog;
